//
//  XZHMusicPlayView.m
//  Piece
//
//  Created by dllo on 16/6/20.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "XZHMusicPlayView.h"
#import "Masonry.h"
static XZHMusicPlayView *playView = nil;

@interface XZHMusicPlayView ()


@end

@implementation XZHMusicPlayView{
    UILabel *subTitle;
    
    UIButton *lastButton;
    UIButton *nextButton;
    UIButton *circulateButton;
    UIButton *singleCirculateButton;
    UIButton *addlistButton;
    UIButton *detailButton;

}
- (void)setMusicArr:(NSArray *)musicArr
{
    if (_musicArr != musicArr) {
        _musicArr = musicArr;
        NSLog(@"aaa%@", _musicArr);
    }
}

+(XZHMusicPlayView *)shareMusicPlayViewInstance
{
    if (playView != nil) {
        return playView;
    }else {
        playView = [[XZHMusicPlayView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        return playView;
    }
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _clearView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _clearView.backgroundColor = [UIColor clearColor];
        [self addSubview:_clearView];
        [self bringSubviewToFront:_clearView];
        
        
        _musicPlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, 180)];
        _musicPlayView.backgroundColor = [UIColor whiteColor];
        [_clearView addSubview:_musicPlayView];


        
        
        _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, ScreenWidth, 44)];
        _backgroundView.backgroundColor = [UIColor whiteColor];
        [_clearView addSubview:_backgroundView];
        [_clearView bringSubviewToFront:_backgroundView];

        _titleLabel = [[UILabel alloc]init];
        _titleLabel.frame = CGRectMake(0, 10, ScreenWidth, 30);
        _titleLabel.textAlignment = 1;
        _titleLabel.text = @"PIECE";
        _titleLabel.font = [UIFont systemFontOfSize:20];
        _titleLabel.textColor = [UIColor colorWithRed:0.23 green:0.22 blue:0.22 alpha:1.00];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
//        _titleLabel.hidden = YES;
        [_backgroundView addSubview:_titleLabel];

        
        subTitle = [[UILabel alloc] init];
        subTitle.textColor = [UIColor colorWithRed:0.46 green:0.75 blue:0.96 alpha:1.00];
        subTitle.text = @"piece";
        subTitle.textAlignment = 1;
        subTitle.font = [UIFont systemFontOfSize:12];
        [_musicPlayView addSubview:subTitle];
        [subTitle makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_musicPlayView).offset(16);
            make.left.equalTo(_musicPlayView);
            make.width.equalTo(ScreenWidth);
            make.height.equalTo(15);
        }];
//        subTitle.hidden = YES;
        
        _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_playButton setBackgroundImage:[UIImage imageNamed:@"playerPause"] forState:UIControlStateNormal];
        [_musicPlayView addSubview:_playButton];
        [_playButton makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo((ScreenWidth - 40) / 2 + 5);
            make.top.equalTo(50);
            make.height.equalTo(40);
            make.width.equalTo(40);
        }];
        [_playButton addTarget:self action:@selector(didClickedPlayButton:) forControlEvents:UIControlEventTouchUpInside];
        //_playButton.hidden = YES;
        
        _pauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_pauseButton setBackgroundImage:[UIImage imageNamed:@"playerPlaying"] forState:UIControlStateNormal];
        [_musicPlayView addSubview:_pauseButton];
        [_pauseButton makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo((ScreenWidth - 40) / 2 + 5);
            make.top.equalTo(50);
            make.height.equalTo(40);
            make.width.equalTo(40);
        }];
        [_pauseButton addTarget:self action:@selector(didClickedPlayButton:) forControlEvents:UIControlEventTouchUpInside];
        _pauseButton.hidden = YES;
        
        
        lastButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [lastButton setBackgroundImage:[UIImage imageNamed:@"previous_normal"] forState:UIControlStateNormal];
        [_musicPlayView addSubview:lastButton];
        [lastButton makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_playButton.left).offset(-30);
            make.top.equalTo(50);
            make.height.equalTo(36);
            make.width.equalTo(36);
        }];
        [lastButton addTarget:self action:@selector(didClickedPreviousButton:) forControlEvents:UIControlEventTouchUpInside];
//        lastButton.hidden = YES;
        
        nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [nextButton setBackgroundImage:[UIImage imageNamed:@"next_nromal"] forState:UIControlStateNormal];
        [_musicPlayView addSubview:nextButton];
        [nextButton makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_playButton.right).offset(23);
            make.top.equalTo(52);
            make.height.equalTo(36);
            make.width.equalTo(36);
        }];
        [nextButton addTarget:self action:@selector(didClickedNextButton:) forControlEvents:UIControlEventTouchUpInside];
//        nextButton.hidden = YES;
        
        circulateButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [circulateButton setBackgroundImage:[UIImage imageNamed:@"player_all_cycle"] forState:UIControlStateNormal];
        [_musicPlayView addSubview:circulateButton];
        [circulateButton makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_musicPlayView).offset(10);
            make.bottom.equalTo(_musicPlayView).offset(-10);
            make.width.equalTo(42);
            make.height.equalTo(42);
        }];
        [circulateButton addTarget:self action:@selector(didClickedCirulateButton:) forControlEvents:UIControlEventTouchUpInside];
//        circulateButton.hidden = YES;
        
        singleCirculateButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [singleCirculateButton setBackgroundImage:[UIImage imageNamed:@"player_single_cycle"] forState:UIControlStateNormal];
        [_musicPlayView addSubview:singleCirculateButton];
        [singleCirculateButton makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_musicPlayView).offset(10);
            make.bottom.equalTo(_musicPlayView).offset(-10);
            make.width.equalTo(42);
            make.height.equalTo(42);
        }];
        [singleCirculateButton addTarget:self action:@selector(didClickedCirulateButton:) forControlEvents:UIControlEventTouchUpInside];
        singleCirculateButton.hidden = YES;
        
        detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [detailButton setBackgroundImage:[UIImage imageNamed:@"detail_content"] forState:UIControlStateNormal];
        [_musicPlayView addSubview:detailButton];
        [detailButton makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_musicPlayView).offset(-10);
            make.bottom.equalTo(_musicPlayView).offset(-10);
            make.width.equalTo(42);
            make.height.equalTo(42);
        }];
        [detailButton addTarget:self action:@selector(didClickedDetailButton:) forControlEvents:UIControlEventTouchUpInside];
//        detailButton.hidden = YES;
        
        addlistButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [addlistButton setBackgroundImage:[UIImage imageNamed:@"music_collection"] forState:UIControlStateNormal];
        [_musicPlayView addSubview:addlistButton];
        [addlistButton makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(detailButton.left).offset(-10);
            make.bottom.equalTo(_musicPlayView).offset(-10);
            make.width.equalTo(42);
            make.height.equalTo(42);
        }];
        [addlistButton addTarget:self action:@selector(didClickedCollectionButton:) forControlEvents:UIControlEventTouchUpInside];
//        addlistButton.hidden = YES;
    }
    return self;
}

- (void)didClickedPlayButton:(UIButton *)sender {
    if (_isPlay) {
        [self.delegate pauseMusic];
        _isPlay = NO;
        _playButton.hidden = NO;
        _pauseButton.hidden = YES;
    }
    else
    {
        [self.delegate playMusic];
        _isPlay = YES;
        _playButton.hidden = YES;
        _pauseButton.hidden = NO;
    }
    
    NSLog(@"播放/暂停");
}

- (void)didClickedPreviousButton:(UIButton *)sender{
    [self.delegate lastMusic];
    NSLog(@"上一曲");
}


- (void)didClickedNextButton:(UIButton *)sender {
    [self.delegate nextMusic];
    NSLog(@"下一曲");
}

- (void)didClickedCirulateButton:(UIButton *)sender{
    if (!_isCirulate) {
        _isCirulate = YES;
        circulateButton.hidden = YES;
        singleCirculateButton.hidden = NO;
    }
    else
    {
        _isCirulate = NO;
        circulateButton.hidden = NO;
        singleCirculateButton.hidden = YES;
    }
    NSLog(@"循环模式");
}

- (void)didClickedCollectionButton:(UIButton *)sender {
    
    NSLog(@"添加至歌单");
}

- (void)didClickedDetailButton:(UIButton *)sender {
    
    NSLog(@"歌曲详情");
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
